/*
 * @Description: 档案备份计划reducer
 * @Author: Rfan
 * @Date: 2022-08-03 20:29:15
 * @LastEditTime: 2022-08-04 11:12:09
 */

import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import {
  ADD_TRANSFER,
  DELETE_TRANSFER,
  EDIT_TRANSFER,
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FOND_ID,
  SET_TRANSFER_LIST
} from '../constants';

export const BackupReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中的全宗
     */
    case SET_SELECT_FOND_ID:
      return {
        ...state,
        selectFondId: action.id
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT_ID: {
      return {
        ...state,
        selectATId: action.id
      };
    }
    // 设置移交清单列表
    case SET_TRANSFER_LIST:
      return {
        ...state,
        transferList: action.data
      };

    // 增加移交清单
    case ADD_TRANSFER:
      return {
        ...state,
        transferList: insertItem(action.data, state.transferList)
      };

    // 增加移交清单
    case EDIT_TRANSFER:
      return {
        ...state,
        transferList: editItemById(action.id, action.data, state.transferList)
      };

    // 删除移交清单
    case DELETE_TRANSFER:
      return {
        ...state,
        transferList: delItemByIdArray(action.ids, state.transferList)
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
